package com.fare.taluo.action.system.register;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.stereotype.Controller;

import com.fare.taluo.BaseFdcAction;
import com.fare.taluo.application.plane.channel.SourceApplication;
import com.fare.taluo.application.system.register.RegistercodeApplication;
import com.fare.taluo.util.DateJsonValueProcessor;
import com.fare.taluo.util.excel.util.StringUtils;
import com.fare.taluo.vo.core.SourceEntityVO;
import com.fare.taluo.vo.registercode.RegisterCodeVO;


/*
 * @xiaozhi
 * Email:ganhongzhi@foreveross.com
 * this is registerCreateCdeAction
*/
@Controller("registerCreateCodeAction")
@Namespace("/")
public class RegisterCreateCodeAction extends BaseFdcAction{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -5064400040739545633L;
	private Long id;	
	private String hbaseKey;	
	private String URLS;	
	private String registerCode;	
	private String name;
	private Date createTime;
	private String hbasekeySels;
	private String data;//JSON
	private RegisterCodeVO saveCodeList;
	private String codes;
	private String days;
	private String hours;
	private String radioHours;
	private String getDays;
	private String getHours;
	private String getRedioHours;
	private Date effectiveDate;
	private String carrierKey;
	
	//返回页面的json格式数据
	private String jsonText;
	
	@Resource(name="registercodeApplication")
	private RegistercodeApplication registercodeApplication;
	
	@Resource(name ="sourceApplication")
	private SourceApplication sourceApplication;
	
	private List<SourceEntityVO> sourceList;
	private SourceEntityVO SourceVo;
	private List<RegisterCodeVO> RegisterCodeList;
	Calendar c = Calendar.getInstance();  
	int dayI=0;
	int hoursI=0;
	int redioHourI=0;
	int allHours=0;
	String urlDate=null;
	private String grabDateFormat = "yyyyMMddhhmmss";
	//CODE生成页面跳转
	public String toRegisterCreateCodeJSP() throws Exception{
		sourceList = sourceApplication.findCrawlAirCompanys();
		String code=null;
		
		if(carrierKey !=null && !"".equals(carrierKey) && StringUtils.isNumber(carrierKey)){
			SourceEntityVO vo=sourceApplication.findByHbaseKey(carrierKey);
			code =vo.getCarrierKey();
		}
		
		RegisterCodeList=registercodeApplication.findRegistercodes(code);
		returnPageURL="/pages/system/register/register_invitationcode.jsp";
		
		JsonConfig jsc = new JsonConfig();
		jsc.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss"));
		this.data=JSONArray.fromObject(this.RegisterCodeList,jsc).toString();
		return "dispatcher";
	}
	//生成URL
	public String createRegisterCode() throws Exception{
		saveCodeList=new RegisterCodeVO();
		hbaseKey =URLDecoder.decode(hbasekeySels,"utf-8");
		SourceVo =sourceApplication.findByHbaseKey(hbaseKey);
		name=SourceVo.getName();
		carrierKey=SourceVo.getCarrierKey();
		
		getDays=URLDecoder.decode(days,"utf-8");
		getHours=URLDecoder.decode(hours,"utf-8");
		getRedioHours=URLDecoder.decode(radioHours,"utf-8");
		if(getDays ==null || "".equals(getDays) || getDays.length()<=0){
			getDays="0";
			
		}
		if(getHours ==null || "".equals(getHours) || getHours.length()<=0 ){
			getHours="0";
			
		}
		if(getRedioHours ==null || "".equals(getRedioHours) || getRedioHours.length()<=0){
			getRedioHours="0";
			
		}
		dayI=Integer.parseInt(getDays);
		hoursI=Integer.parseInt(getHours);
		redioHourI=Integer.parseInt(getRedioHours);
		 
		allHours=hoursI+redioHourI;
		
		//组装有效日期
		c.setTime(new Date());
		c.add(Calendar.DATE,dayI);
		c.add(Calendar.HOUR,allHours);
		
		effectiveDate = c.getTime(); //结果   
		
	//	urlDate=DateUtils.dateToStr(effectiveDate, grabDateFormat);

		//随机生成数字作为code 
		long random= (long)(Math.random()*9999+1000);
		 String randomCode=String.valueOf(random);
		String rAndHb=hbaseKey+randomCode;
		
		String url_1="";
		HttpServletRequest request = ServletActionContext.getRequest();
		if(request.getHeader("host").equals("localhost:8080")){
			url_1="mofang-web/register/input.action";//本地测试专用 
		}else{
			url_1="register/input.action";//正式版本应用
		}
		URLS=request.getScheme()+"://";   
		URLS+=request.getHeader("host"); 
		URLS+= "/"+url_1+"?code="+rAndHb;
	//	URLS+= "/"+url_1+"?code="+rAndHb+"&effectiveDate="+urlDate;

		
        try{
        saveCodeList.setName(name);
		saveCodeList.setCreateTime(new Date());
		saveCodeList.setHbaseKey(hbaseKey);
		saveCodeList.setRegisterCode(rAndHb);
		saveCodeList.setURLS(URLS);
		saveCodeList.setCarrierKey(carrierKey);
		saveCodeList.setEffectiveDate(effectiveDate);
        }catch (Exception e) {
			System.out.println(e.getMessage());
		}
		
		registercodeApplication.saveURLS(saveCodeList);
		toRegisterCreateCodeJSP();
//		returnPageURL = "/sign/registerCode/registerCreate.jsp";
		//return "dispatcher";
		return toRegisterCreateCodeJSP();
	}
	public void checkCode() throws IOException{
		List<RegisterCodeVO> list = new ArrayList<RegisterCodeVO>();
		HttpServletResponse response=ServletActionContext.getResponse();
		String hbaseKeys=null;
		String code=null;
		Date effectiveDates =null;
		try {
			hbaseKeys = URLDecoder.decode(hbaseKey,"utf-8");
			code=URLDecoder.decode(codes,"utf-8");
			
			//根据CODE来判断有效注册日期是否有效
			//1.根据CODE条件来查找有效注册日期
			/*RegisterCodeVO	voDatas =registercodeApplication.getCodeDateByCode(codes);
			effectiveDates=voDatas.getEffectiveDate();*/
			
			//2.从查找出的有效日期与现在时间对比
			
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		//request.getParameter(hbaseKey);
		list=registercodeApplication.checkCode(code, hbaseKeys);
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class,new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss"));
		JSONArray jsarry=JSONArray.fromObject(list,jsonConfig);
		jsonText=jsarry.toString();

	    response.setContentType("application/json;charset=utf-8"); 
		response.getWriter().write(jsonText);
	}
	
	
	public List<SourceEntityVO> getSourceList() {
		return sourceList;
	}

	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
		
	public String getHbaseKey() {
		return hbaseKey;
	}

	public void setHbaseKey(String hbaseKey) {
		this.hbaseKey = hbaseKey;
	}

	public String getURLS() {
		return URLS;
	}
	public void setURLS(String uRLS) {
		URLS = uRLS;
	}
	public String getRegisterCode() {
		return registerCode;
	}
	public void setRegisterCode(String registerCode) {
		this.registerCode = registerCode;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Date getCreateTime() {
		return createTime;
	}
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	public List<RegisterCodeVO> getRegisterCodeList() {
		return RegisterCodeList;
	}
	public void setRegisterCodeList(List<RegisterCodeVO> registerCodeList) {
		RegisterCodeList = registerCodeList;
	}
	public RegistercodeApplication getRegistercodeApplication() {
		return registercodeApplication;
	}
	public void setRegistercodeApplication(
			RegistercodeApplication registercodeApplication) {
		this.registercodeApplication = registercodeApplication;
	}
	public String getHbasekeySels() {
		return hbasekeySels;
	}
	public void setHbasekeySels(String hbasekeySels) {
		this.hbasekeySels = hbasekeySels;
	}
	public String getData() {
		return data;
	}
	public void setData(String data) {
		this.data = data;
	}

	public RegisterCodeVO getSaveCodeList() {
		return saveCodeList;
	}

	public void setSaveCodeList(RegisterCodeVO saveCodeList) {
		this.saveCodeList = saveCodeList;
	}
	public String getCodes() {
		return codes;
	}
	public void setCodes(String codes) {
		this.codes = codes;
	}
	public SourceEntityVO getSourceVo() {
		return SourceVo;
	}
	
	public String getJsonText() {
		return jsonText;
	}
	public void setJsonText(String jsonText) {
		this.jsonText = jsonText;
	}
	public Date getEffectiveDate() {
		return effectiveDate;
	}
	public void setEffectiveDate(Date effectiveDate) {
		this.effectiveDate = effectiveDate;
	}
	public String getDays() {
		return days;
	}
	public void setDays(String days) {
		this.days = days;
	}
	public String getHours() {
		return hours;
	}
	public void setHours(String hours) {
		this.hours = hours;
	}
	public String getRadioHours() {
		return radioHours;
	}
	public void setRadioHours(String radioHours) {
		this.radioHours = radioHours;
	}
	public String getGetDays() {
		return getDays;
	}
	public void setGetDays(String getDays) {
		this.getDays = getDays;
	}
	public String getGetHours() {
		return getHours;
	}
	public void setGetHours(String getHours) {
		this.getHours = getHours;
	}
	public String getGetRedioHours() {
		return getRedioHours;
	}
	public void setGetRedioHours(String getRedioHours) {
		this.getRedioHours = getRedioHours;
	}
	public String getCarrierKey() {
		return carrierKey;
	}
	public void setCarrierKey(String carrierKey) {
		this.carrierKey = carrierKey;
	}
	
	
}
